import { Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy } from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";
import {GoodsService} from "@goods/goods.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {CouponRespVO} from "@interface/index";



@Component({
  selector: 'coupon-detail',
  templateUrl: './coupon-detail.component.html',
  styleUrls: ['./coupon-detail.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class CouponDetailComponent implements OnInit {
  public codeId: number;
  public status: string;
  public coupon: CouponRespVO;
  public couponStatusList: any = [
    { tag: 1, name: '未使用'},
    { tag: 2, name: '已使用'},
    { tag: 4, name: '已过期'},
    { tag: 5, name: '已作废'},
    { tag: 6, name: '最新'},
    { tag: 7, name: '将过期'},
  ];
  constructor(private route: ActivatedRoute,
              public router: Router,
              public goodsService: GoodsService,
              public toolsService: ToolsService ) {
    this.route.params.subscribe((params: Params) => {
      this.codeId = params['codeId'] ;
      this.getCouponDetail();
    });
  }

  /**
   * @description 优惠券详情
   * @author @zhupw1360
   * @date 2018-01-04
   * @param params {  page , rows }
   */
  getCouponDetail() {
    this.goodsService.getCouponDetail(this.codeId)
      .subscribe(res => {
      if (res.result === '0' && res.data ) {
        this.coupon = res.data;
        // console.log(this.coupon);
        this.handleTagCouponStatus(this.coupon.tag);
      }
    });
  }

  /**
   * @description 处理优惠券的不同状态名称
   * @author @zhupw-1360
   * @date 2018-01-24
   * @param tag
   */
  handleTagCouponStatus(tag) {
    this.couponStatusList.forEach((item) => {
      if (item['tag'] === tag) {
        this.status = item['name'];
      }
    });
  }

  ngOnInit() {
  }

}
